package com.shanghaionstar.smart.util;

import android.app.Activity;
import android.content.Context;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.net.wifi.ScanResult;
import android.net.wifi.WifiManager;
import android.os.Build;
import android.text.TextUtils;

import com.midea.iot.sdk.cloud.openapi.MSmartSDK;

import java.util.List;

/**
 * Created by Rengui Lu on 2016/6/17.
 */
public class NetUtil {
    private static WifiManager getWifiManager(Context context){
        WifiManager mWifiManager = (WifiManager) context.getSystemService(Activity.WIFI_SERVICE);
        return mWifiManager;
    }

    public static ScanResult getScanResultBySsid(String ssid,Context context){
        ScanResult mResult = null;
        WifiManager mWifiManager = (WifiManager) context.getSystemService(Activity.WIFI_SERVICE);
        List<ScanResult> results = mWifiManager.getScanResults();
        for(ScanResult result : results){
            if(result.SSID.equalsIgnoreCase(ssid)){
                mResult = result;
                break;
            }
        }
        return  mResult;
    }


    public static String  getMediaSsid(String mQRcode,Context context) {
        String ssid = MSmartSDK.getInstance().getThirdManager().getSSIDFromQRCode(mQRcode);
        if(TextUtils.isEmpty(ssid)){
            WifiManager mWifiManager = (WifiManager) context.getSystemService(Activity.WIFI_SERVICE);
            List<ScanResult> results = mWifiManager.getScanResults();
            if(results.size() == 0)
                return "";
            for(ScanResult result : results){
                if(result.SSID.contains("midea")){
                    ssid = result.SSID;
                    break;
                }
            }
        }
        return ssid;
    }

    public static boolean containSsid(String ssid,Context context){
        boolean contain = false;
        WifiManager mWifiManager = (WifiManager) context.getSystemService(Activity.WIFI_SERVICE);
        List<ScanResult> results = mWifiManager.getScanResults();
        if(results.size() == 0)
            return true;

        for(ScanResult result : results){
            if(result.SSID.equalsIgnoreCase(ssid)){
                contain = true;
                break;
            }
        }
        return  contain;
    }

    /**
     * 判断wifi是否打开
     * @param context
     * @return
     */
    public static boolean isWifiEnabled(Context context){
        return getWifiManager(context).isWifiEnabled();
    }

    /**
     * 判断wifi是否连上
     * @param context
     * @return
     */
    public static boolean isWifiConnected(Context context){

        if (!isWifiEnabled(context)){
            return false;
        }
        ConnectivityManager mConnectivityManager = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
        NetworkInfo networkInfo = mConnectivityManager.getActiveNetworkInfo();
        return networkInfo != null && (networkInfo.getType() == ConnectivityManager.TYPE_WIFI) && networkInfo.isConnected();
    }

    public static String getCapabilities(String ssid,Context context){
        String cap="";
        WifiManager mWifiManager = (WifiManager) context.getSystemService(Activity.WIFI_SERVICE);
        List<ScanResult> results = mWifiManager.getScanResults();
        for(ScanResult result : results){
            if(result.SSID.equalsIgnoreCase(ssid)){
               cap =result.capabilities;
                break;
            }
        }
        return  cap;
    }

    public static String getCurrentSSID(Context context){
        if (isWifiConnected(context)) {
            WifiManager mWifiManager = (WifiManager) context.getSystemService(Activity.WIFI_SERVICE);
            String ssid = mWifiManager.getConnectionInfo().getSSID();

            if (TextUtils.isEmpty(ssid)){
                return null;
            }
            if (Build.VERSION.SDK_INT >= 17) {
                if (ssid.startsWith("\"") && ssid.endsWith("\"")) {
                    ssid = ssid.substring(1, ssid.length() - 1);
                }
            }
            return ssid;
        }
        return null;
    }
//    public static boolean isWifiConnected(Context context) {
//        ConnectivityManager connectivityManager = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
//        NetworkInfo wifiNetworkInfo = connectivityManager.getNetworkInfo(ConnectivityManager.TYPE_WIFI);
//        if (wifiNetworkInfo.isConnected()) {
//            return true;
//        }
//        return false;
//    }
//
//    public static String getWifiSsid(Context context) {
//        WifiManager wifiManager = (WifiManager) context.getSystemService(Context.WIFI_SERVICE);
//        WifiInfo wifiInfo = wifiManager.getConnectionInfo();
//        String ssid = "";
//        if (wifiInfo != null)
//            ssid = wifiInfo.getSSID();
//        return ssid;
//    }

}
